package com.navinfo.platform.bigdata.hive.udf;


import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.IntWritable;

/**
 * 按照权重和总数来计算值
 */
@Description(name = "status",
        value = "_FUNC_(total, curValue, other) - returns curValue real value by percent,total*(curValue/(curValue+other))",
        extended = "Example:\n   " +
                "> select dataplatform.amendDataByWeight(120,3,1,2);;\n"+
                "> select dataplatform.amendDataByWeight(120,3,1,2);;"
)
public class AmendDataByWeightUDF extends UDF {

    /**
     * 获取num第index位的bit值
     * @param total
     * @param first
     * @return
     */
    public IntWritable evaluate(IntWritable total, IntWritable curValue, IntWritable ... other) {
        int sum = curValue.get();
        for(IntWritable data : other){
            sum += data.get();
        }
        int admendData = (int) Math.round(total.get() * (curValue.get() * 1.0/sum));
        return new IntWritable(admendData);
    }
}
